<?php
final class Epic
{  
	/**
	 * Js url
	 * @var string
	 */
    const JS_URL                  = 'js';
    
    /**
     * Css url
     * @var string
     */
    const CSS_URL                 = 'css';
    
    /**
     * Img url
     * @var string
     */
    const IMG_URL                 = 'img';
        
    /**
     * Get base url
     *
     * param   string|null $type
     * @return string
     */
    public static function getBaseUrl($type = null)
    {
    	$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == 'https://' ? 'https://' : 'http://'; 
    	$hostName = $_SERVER['HTTP_HOST'];
    	$pathInfo = pathinfo($_SERVER['PHP_SELF']);
    	
    	if($pathInfo['dirname'] == '/')
    		$pathInfo['dirname'] = '';
    		
        $url = $protocol.$hostName.$pathInfo['dirname'];
    	
        if($type != null) {
        	return $url . '/' . $type . '/' . 'base' . '/';
        } else {
        	return $url . '/';
        }
    }
    
    /**
     * Get Js url by route
     *
     * @param   string $route
     * @return  string
     */
    public static function getJsUrl($route = '')
    {
       	return self::getBaseUrl(self::JS_URL) . $route;

    }
	
	/**
     * Get list Js url by route
     *
     * @param   string $route
     * @return  string
     */
    public static function getListJsUrl()
    {
		global $url_js;
       	return $url_js;
    }
	
    /**
     * add Js url by route
     *
     * @param   string $route
     * @return  string
     */
    public static function addListJsUrl($route)
    {
		global $url_js;
		$url_js[] = self::getJsUrl ($route);
    }
	/**
     * Get list Js url by route
     *
     * @param   string $route
     * @return  string
     */
    public static function getListCssUrl()
    {
		global $url_css;
       	return $url_css;
    }
	
    /**
     * add Js url by route
     *
     * @param   string $route
     * @return  string
     */
    public static function addListCssUrl($route)
    {
		global $url_css;
		$url_css[] = self::getCssUrl ($route);
    }
	
    /**
     * Get Css url by route
     *
     * @param   string $route
     * @return  string
     */
    public static function getCssUrl($route = '')
    {
       	return self::getBaseUrl(self::CSS_URL) . $route;
    }
    
    /**
     * Get Img url by route
     *
     * @param   string $route
     * @return  string
     */
    public static function getImgUrl($route = '')
    {
       	return self::getBaseUrl(self::IMG_URL) . $route;
    }
    
    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public static function getUrl($route = '', $params = array())
    {
    	$param = '';
    	if ($params) { 
    		$param = '?';
    		foreach ($params as $key => $value)
    			$param .= $key . '=' . $value . '&';
    		$param = trim($param, "&");
    	}
    	return self::getBaseUrl() . $route . $param;
    }
    
    /**
     * Generate current url by parameters
     *
     * @param   array $params
     * @return  string
     */
    public static function getCurrentUrl($params = array())
    {    
    	// router
    	if (isset($_SERVER['REDIRECT_URL'])) {
	    	$fragments = explode('/', trim($_SERVER['REDIRECT_URL'], '/'));
	    	$pathInfo = pathinfo($_SERVER['PHP_SELF']);
	    	if($pathInfo['dirname'] != '/') {
	    		array_shift($fragments);
	    	}
	    	$route = implode('/', $fragments);
    	} else {
    		$config = Epic::getRegistry()->get('config');
    		$route = $config->getDefaultModule() . '/'
    		       . $config->getDefaultController() . '/'
    		       . $config->getDefaultAction();
    	}
    	
    	// param
    	$param = '';
    	if ($params) {
    		$param = '?';
    		foreach ($params as $key => $value)
    			$param .= $key . '=' . $value . '&';
    		$param = trim($param, "&");
    	}
    	
    	return self::getBaseUrl() . $route . $param;
    }
    
    
    
    /**
     * Retrive adapter object
     *
     * @return Epic_Db_Adapter_Abstract
     */
    public static function getAdapter()
    {
    	$config = self::getRegistry()->get('config');
    	return Epic_Db_Adapter_Mysqli::getInstance(array($config->getConfig('database/db_hostname'),
    			$config->getConfig('database/db_username'),
    			$config->getConfig('database/db_password'),
    			$config->getConfig('database/db_name')));
    }
    
    /**
     * Retrive model class used factory method
     *
     * @param string $path path to model class
     * @return Epic_Db_Data_Abstract
     */
    public static function getModel($path)
    {
    	list($modlueName, $modelName) = explode("/", $path);
    	require_once(ucfirst($modlueName).'/models/'.ucfirst($modelName).'.php');
    	return new $modelName;
    }
    
    /**
     * Retrive registry instance
     * 
     * @return Epic_Registry_Abstract
     */
    public static function getRegistry()
    {
    	return Epic_Registry_App::getInstance();
    }
    
    /**
     * Retrive Session registry instance
     *
     * @return Epic_Registry_Abstract
     */
    public static function getSession()
    {
    	return Epic_Registry_Session::getInstance();
    }
    
    /**
     * Utility function add successful message
     * 
     * @param string $value successful message
     */
    public static function setSuccess($value)
    {
    	self::getSession()->set('successful', $value);
    }
    
    /**
     * Utility function add error message
     *
     * @param string $value error message
     */
    public static function setError($value)
    {
    	self::getSession()->set('error', $value);
    }
    
    /**
     * Utility function add form data
     *
     * @param mixed $value form data
     */
    public static function setFormData($value)
    {
    	self::getSession()->set('formData', $value);
    }
    
    /**
     * Utility function get form data
     *
     * @param mixed $key
     * @return mixed 
     */
    public static function getFormData($key)
    {
    	$formData = self::getSession()->get('formData');
    	return $formData[$key];
    }
    
    /**
     * get title for web
     * 
     */
    public static function getTitle () {
    	global $title;
    	return $title;
    } 
    /**
     * set title 
     * @return unknown
     */
	public static function setTitle ($title1) {
    	global $title;
    	$title = $title1;
    }
    
    /**
     * for upload file 
     * 
     */
   	public static function uploadFile ($name,$nameRoot,$fileName) {
   		$tmp_name = $_FILES[$name]["tmp_name"];
   		move_uploaded_file ($tmp_name,$nameRoot."/".$fileName);
   	}
}
    